################################################################################
#  ETFAna project, Anyang Normal University && IMP-CAS
#  \brief For building executables that do not want to be run as macros here.
#  \author SUN Yazhou, asia.rabbit@163.com
#  \since 2021-10-06
#  \date 2024-06-14
#  \attention
#  changelog
#  <table>
#  <tr>  <th>Date         <th>Author      <th>Description                  </tr>
#  <tr>  <td>2021-10-06   <td>Asia Sun    <td>file created                 </tr>
#  </table>
#  \copyright Copyright (c) 2021-2024 Anyang Normal U. && IMP-CAS with LGPLv3 LICENSE
################################################################################

cmake_minimum_required(VERSION 3.12 FATAL_ERROR)
project(ETFAna VERSION 3.0.0)

find_program(CLANG_EXECUTABLE clang++)
find_program(GXX_EXECUTABLE g++)
if(CLANG_EXECUTABLE)
  set(CMAKE_CXX_COMPILER "clang++")
elseif(GXX_EXECUTABLE)
  set(CMAKE_CXX_COMPILER "g++")
else()
  message(FATAL_ERROR "No CXX compilers were found")
endif()

set(LIBRARY_OUTPUT_PATH ${CMAKE_BINARY_DIR}/lib)
set(EXECUTABLE_OUTPUT_PATH ${CMAKE_BINARY_DIR})
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/cmake/modules")
# Build Type: MinSizeRel Debug Release RelWithDebInfo
# set(BUILD_TYPE Debug) # appropriate for DEBUG, yet rather slow
set(BUILD_TYPE Release) # compiler-optimized, extreme performance
# set(BUILD_TYPE MinSizeRel) # rel, and with minized lib and exe size
# set(BUILD_TYPE RelWithDebInfo) # rel, and with DEB info
if(NOT CMAKE_BUILD_TYPE)
  set(CMAKE_BUILD_TYPE ${BUILD_TYPE})
else()
  set(BUILD_TYPE ${CMAKE_BUILD_TYPE})
endif()

find_package(ROOT REQUIRED)
link_directories(${ROOT_LIBRARY_DIR})
include_directories(${ROOT_INCLUDE_DIRS})
link_libraries(Core Hist Tree Gui yaml-cpp rt)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${ROOT_CXX_FLAGS} -Wall")

include(ETFMacros) # Macros dedicated for ETFAna

# swith option for catch2 -- a 3rd-party cpp unit-test package
option(_CATCH_ "CATCH2 TEST" OFF)
# message(STATUS "CATCH UNIT TEST: ${_CATCH_}")
# message(STATUS "  (Turn on the catch unittest with -D_CATCH_=ON)")

# for selection of ROOT-independent code configuration in ETFAna (e.g., online)
option(_ROOT_ "ROOT Framework" ON)

# add your own subdirectories that contain your codes
add_subdirectory(base)
add_subdirectory(user)
add_subdirectory(macro)

# for setting drawing styles
file(COPY ${CMAKE_SOURCE_DIR}/config/rootlogon.C DESTINATION ${CMAKE_BINARY_DIR})
# many useful macros
file(GLOB VAR ${CMAKE_SOURCE_DIR}/config/*.sh)
file(COPY ${VAR} DESTINATION ${CMAKE_BINARY_DIR})
# the online configuration file
file(COPY ${CMAKE_SOURCE_DIR}/config/ipc.yaml DESTINATION ${CMAKE_BINARY_DIR})

# print the build type
string(TOUPPER ${BUILD_TYPE} BUILD_TYPE_UPPER)
execute_process(
  COMMAND ${CMAKE_CXX_COMPILER} -dumpversion
  OUTPUT_VARIABLE COMPILER_VERSION
)
string(STRIP ${COMPILER_VERSION} COMPILER_VERSION_STRIPPED)
message(STATUS "======= BUILD TYPE ===========")
message(STATUS "CMAKE_C_COMPILER: ${CMAKE_C_COMPILER}")
message(STATUS "CMAKE_CXX_COMPILER: ${CMAKE_CXX_COMPILER} ${COMPILER_VERSION_STRIPPED}")
message(STATUS "Build Type: ${CMAKE_BUILD_TYPE}")
message(STATUS "CMAKE_CXX_FLAGS: ${CMAKE_CXX_FLAGS}   ${CMAKE_CXX_FLAGS_${BUILD_TYPE_UPPER}}")
message(STATUS "(TIPS: Change with -DCMAKE_BUILD_TYPE=<build_type>)")
message(STATUS "(TIPS: Available build_type-s: MinSizeRel Debug Release RelWithDebInfo)")
message(STATUS "======= BUILD TYPE ===========")
